{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9b37dca3-e7dc-4999-82bc-c2f121b0c8fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test 1: Both empyt\n",
      "Merge and sort the list\n",
      "2->3->4->5->6->END\n"
     ]
    }
   ],
   "source": [
    "class ListNode:\n",
    "    def __init__(self,value=0,next=None):\n",
    "        self.value = value\n",
    "        self.next = next\n",
    "\n",
    "def merge_two_sort_lists(l1,l2):\n",
    "    dummy = ListNode(0)\n",
    "    current = dummy\n",
    "\n",
    "    #merge the listnode\n",
    "    while l1 and l2:\n",
    "        if l1.value < l2.value:\n",
    "            current.next = l1\n",
    "            l1 = l1.next\n",
    "        else: \n",
    "            current.next = l2\n",
    "            l2 = l2.next\n",
    "        current = current.next\n",
    "\n",
    "    if l1:\n",
    "        current.next = l1\n",
    "    if l2:\n",
    "        current.next = l2\n",
    "\n",
    "    return dummy.next\n",
    "\n",
    "def print_list(head):\n",
    "    current = head\n",
    "    while current:\n",
    "        print(current.value, end=\"->\")\n",
    "        current = current.next\n",
    "    print(\"END\")\n",
    "\n",
    "# l1 = ListNode(1)\n",
    "# l1.next = ListNode(3)\n",
    "# l1.next.next = ListNode(5)\n",
    "\n",
    "# l2 = ListNode(2)\n",
    "# l2.next = ListNode(4)\n",
    "# l2.next.next = ListNode(6)\n",
    "\n",
    "# merge_list = merge_two_sort_lists(l1,l2)\n",
    "\n",
    "# print(\"Merge and sort the list\")\n",
    "# print_list(merge_list)\n",
    "\n",
    "\n",
    "\n",
    "# 1:all empty:\n",
    "# print(\"Test 1: Both empyt\")\n",
    "# print_list(merge_list(None,None))\n",
    "\n",
    "# merge_list = merge_two_sort_lists(l1,l2)\n",
    "\n",
    "# print(\"Merge and sort the list\")\n",
    "# print_list(merge_list)\n",
    "\n",
    "\n",
    "# 2:Only one empty:\n",
    "print(\"Test 1: Both empyt\")\n",
    "l1 = None\n",
    "\n",
    "merge_list = merge_two_sort_lists(l1,l2)\n",
    "\n",
    "print(\"Merge and sort the list\")\n",
    "print_list(merge_list)\n",
    "\n",
    "\n",
    "# # 3:Not same len:\n",
    "# print(\"Test 1: Both empyt\")\n",
    "\n",
    "# l1 = ListNode(1)\n",
    "# l1.next = ListNode(3)\n",
    "# l1.next.next = ListNode(5)\n",
    "\n",
    "\n",
    "\n",
    "# l2 = ListNode(2)\n",
    "# l2.next = ListNode(4)\n",
    "# l2.next.next = ListNode(6)\n",
    "# l2.next.next.next = ListNode(8)\n",
    "\n",
    "\n",
    "\n",
    "# merge_list = merge_two_sort_lists(l1,l2)\n",
    "\n",
    "# print(\"Merge and sort the list\")\n",
    "# print_list(merge_list)\n",
    "\n",
    "# # 3:Have the same len but some overlapping\n",
    "\n",
    "\n",
    "# l1 = ListNode(1)\n",
    "# l1.next = ListNode(3)\n",
    "# l1.next.next = ListNode(5)\n",
    "\n",
    "\n",
    "\n",
    "# l2 = ListNode(1)\n",
    "# l2.next = ListNode(2)\n",
    "# l2.next.next = ListNode(5)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# merge_list = merge_two_sort_lists(l1,l2)\n",
    "\n",
    "\n",
    "# # 4: One is in other\n",
    "\n",
    "# l1 = ListNode(1)\n",
    "# l1.next = ListNode(3)\n",
    "# l1.next.next = ListNode(5)\n",
    "\n",
    "\n",
    "\n",
    "# l2 = ListNode(1)\n",
    "# l2.next = ListNode(3)\n",
    "# l2.next.next = ListNode(5)\n",
    "# l2.next.next.next = ListNode(7)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# merge_list = merge_two_sort_lists(l1,l2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3749803b-1d47-4e5b-a62c-bd6534b373c0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
